﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MVCClubSite.IDAL;
using MVCClubSite.Model;
using MVCClubSite.BLL;
using MVCClubSite.Web.ViewData;
using MVCClubSite.Helpers;

namespace MVCClubSite.Web.Controllers
{
    public class LinksController : BaseController
    {
        LinksService _linksService;

        public LinksController()
            : this(null)
        {
        }

        public LinksController(LinksService linksService)
        {
            _linksService = linksService ?? new LinksService();
        }

        public ActionResult List()
        {
            LinkListViewData data = new LinkListViewData();
            data.Links = _linksService.GetLinks();

            return View("List", data);
        }

        [AcceptVerbs("GET")]
        public ActionResult New()
        {
            return View("New");
        }

        [AcceptVerbs("POST")]
        public ActionResult Create(FormCollection form)
        {
            Link newLink = new Link();
            this.UpdateModel(newLink, new[] { "Title", "Description", "Url", "Sequence" });

            HttpPostedFileBase posted = Request.Files["file"];
            if (posted != null && posted.ContentLength > 0)
            {
                newLink.Image = new byte[posted.ContentLength];
                System.IO.Stream myStream = posted.InputStream;
                myStream.Read(newLink.Image, 0, posted.ContentLength);
            }

            _linksService.Save(newLink);

            return RedirectToAction("List");
        }

        [AcceptVerbs("GET")]
        public ActionResult Edit(string id)
        {
            LinkViewData data = new LinkViewData();
            int linkID = int.Parse(id);
            data.Link = _linksService.GetLinkByID(linkID);

            return View("Edit", data);
        }

        [AcceptVerbs("POST")]
        public ActionResult Update(FormCollection form, int id)
        {
            Link existingLink = _linksService.GetLinkByID(id);
            this.UpdateModel(existingLink, new[] { "Title", "Description", "Url", "Sequence" });

            if (bool.Parse(form["deleteImage"]))
                existingLink.Image = null;

            HttpPostedFileBase posted = Request.Files["file"];
            if (posted != null && posted.ContentLength > 0)
            {
                existingLink.Image = new byte[posted.ContentLength];
                System.IO.Stream myStream = posted.InputStream;
                myStream.Read(existingLink.Image, 0, posted.ContentLength);
            }

            _linksService.Save(existingLink);

            return RedirectToAction("List");
        }

        [AcceptVerbs("POST")]
        public ActionResult Delete(int id)
        {
            _linksService.Delete(id);
            return RedirectToAction("List");
        }
    }
}
